﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;

namespace TestMongoDb
{
    public class MongoDbHelper
    {
        private MongoClient client;
        private IMongoDatabase db;
        private IMongoCollection<BsonDocument> collection;

        public MongoDbHelper(string dbname, string tableName)
        {
            client = new MongoClient("mongodb://localhost:27017");
            db = client.GetDatabase(dbname);
            collection = db.GetCollection<BsonDocument>(tableName);
        }

        /// <summary>
        /// 添加一条
        /// </summary>
        /// <param name="doc">新增bson文档</param>
        public void InsertOne(BsonDocument doc)
        {
            collection.InsertOne(doc);
        }


        /// <summary>
        /// 添加多条
        /// </summary>
        /// <param name="docs">新增bson文档集合</param>
        public void InsertManay(IEnumerable<BsonDocument> docs)
        {
            collection.InsertMany(docs);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <param name="filterDoc">查询语句</param>
        /// <returns></returns>
        public long GetCount(FilterDefinition<BsonDocument> filterDoc)
        {
            return collection.Count(filterDoc);
        }

        /// <summary>
        /// 获取一条
        /// </summary>
        /// <param name="filterDoc">查询</param>
        /// <param name="sortDoc">排序</param>
        /// <param name="projectDoc">返回特定字段</param>
        /// <returns></returns>
        public BsonDocument FindOne(FilterDefinition<BsonDocument> filterDoc,SortDefinition<BsonDocument> sortDoc=null,ProjectionDefinition<BsonDocument> projectDoc=null )
        {
            return collection.Find(filterDoc).Project(projectDoc??new BsonDocument()).Sort(sortDoc?? new BsonDocument()).First();
        }

        /// <summary>
        /// 获取多条
        /// </summary>
        /// <param name="filterDoc">查询</param>
        /// <param name="sortDoc">排序</param>
        /// <param name="projectDoc">返回特定字段</param>
        /// <returns></returns>
        public IEnumerable<BsonDocument> FindMore(FilterDefinition<BsonDocument> filterDoc, SortDefinition<BsonDocument> sortDoc = null, ProjectionDefinition<BsonDocument> projectDoc = null)
        {
            return collection.Find(filterDoc).Project(projectDoc ?? new BsonDocument()).Sort(sortDoc ?? new BsonDocument()).ToList();
        }

        /// <summary>
        /// 更新一条
        /// </summary>
        /// <param name="filterDoc">查询</param>
        /// <param name="updateDoc">更新内容</param>
        public UpdateResult Update(FilterDefinition<BsonDocument> filterDoc, UpdateDefinition<BsonDocument> updateDoc)
        {
            return collection.UpdateOne(filterDoc, updateDoc);
        }

        /// <summary>
        /// 更新多条
        /// </summary>
        /// <param name="filterDoc">查询</param>
        /// <param name="updateDoc">更新内容</param>
        /// <returns></returns>
        public UpdateResult UpdateMore(FilterDefinition<BsonDocument> filterDoc, UpdateDefinition<BsonDocument> updateDoc)
        {
            return collection.UpdateMany(filterDoc, updateDoc);
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="filterDoc">删除条件</param>
        public DeleteResult DeleteOne(FilterDefinition<BsonDocument> filterDoc)
        {
            return collection.DeleteOne(filterDoc);
        }

        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="filterDoc">删除条件</param>
        /// <returns></returns>
        public DeleteResult DeleteMore(FilterDefinition<BsonDocument> filterDoc)
        {
            return collection.DeleteMany(filterDoc);
        }
    }
}
